<?php
//占位
//颜色转换
trait trait_gdcolor
{
	function formatColor2int($char)
	{
		static $array = array('a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, );
		return isset($array[$char]) ? $array[$char] : $char;
	}
//-------------------------------------------------------------------------------
	function formatColor2char($int)
	{
		static $array = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f', );
		return isset($array[$int]) ? $array[$int] : $int;
	}
//-------------------------------------------------------------------------------

	function formatColor($color)
	{
		if($color == '') return '000000';
		
		$color = strtolower($color);
		
		if($color{0} === '#')
			$color = substr($color, 1);
		
		if(!isset($color{3}))
		{
			$color = $color{0} . $color{0} . $color{1} . $color{1} . $color{2} . $color{2};
		}
		
		return $color;
	}
//-------------------------------------------------------------------------------

	//16进制转10进制
	function color2rgb($color)
	{
		$color = $this->formatColor($color);
		$array = str_split($color, 2);
		return array_map('hexdec', $array);
	}
//-------------------------------------------------------------------------------

	//rgb转索引
	function rgb2index($r, $g, $b)
	{
		return imagecolorallocate($this->handle, $r, $g, $b);
	}
//-------------------------------------------------------------------------------

	//索引转rgb
	function index2rgb($int)
	{
		$array = imagecolorsforindex($this->handle, $int);
		return array($array['red'], $array['green'], $array['blue']);
	}

//-------------------------------------------------------------------------------

	//rgb(rgb是十进制表示的)转16进制
	function rgb2color($r, $g, $b)
	{
		$r = ($r < 16 ? '0' : '') . dechex($r);
		$g = ($g < 16 ? '0' : '') . dechex($g);
		$b = ($b < 16 ? '0' : '') . dechex($b);
		return $r . $g . $b;
	}

//-------------------------------------------------------------------------------

	//直接从颜色转换到素引
	function color2index($color)
	{
		list($r, $g, $b) = $this->color2rgb($color);
		return $this->rgb2index($r, $g, $b);
	}
//-------------------------------------------------------------------------------

	//直接从素引转换到颜色
	function index2color($int)
	{
		list($r, $g, $b) = $this->index2rgb($int);
		return $this->rgb2color($r, $g, $b);
	}
//-------------------------------------------------------------------------------

}